package com.mjk.common.web.getway.restful.interceptor.type.imports.impl;


import cn.hutool.core.io.FileUtil;
import com.mjk.common.base.response.BeeResponse;
import com.mjk.common.tools.excel.type.FileType;
import com.mjk.common.tools.interceptor.InterceptorChain;
import com.mjk.common.tools.logger.BizLogger;
import com.mjk.common.tools.task.ITask;
import com.mjk.common.tools.task.multitask.MultiAnsyTaskExecute;
import com.mjk.common.util.EncodingDetect;
import com.mjk.common.util.FileUtils;
import com.mjk.common.web.getway.restful.interceptor.GetWayInterceptorReq;
import com.mjk.common.web.getway.restful.interceptor.annotation.BeeGetWayInterceptor;
import com.mjk.common.web.getway.restful.interceptor.type.GetWayInterceptor;
import com.mjk.common.web.getway.restful.interceptor.type.GetWayInterceptorType;
import com.mjk.common.web.getway.transfer.GetwayTransfer;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 文件导入拦截器 文件切割/分片调用
 *
 * @author mjk
 */
//@BeeGetWayInterceptor(value = 1, interceptorType = GetWayInterceptorType.IMPORT)
public class ImportFileSplitInterceptor implements GetWayInterceptor {

	public static final long SPLIX_FILE_SIZE = 5 * 1024 * 1024L;

	@Override
	public BeeResponse interceptor(GetWayInterceptorReq request,
								   InterceptorChain<GetWayInterceptorReq, BeeResponse> next) {
		List<String> fileNames = new ArrayList<>();
		if (request.getFileName().contains(",")) {
			for (String fileName : request.getFileName().split(",")) {
				try {
					fileNames = FileUtils.splitFile(fileName, SPLIX_FILE_SIZE);
				} catch (Exception e) {
					BizLogger.error("file split is error");
					fileNames.add(fileName);
				}
			}
		} else {
			fileNames.add(request.getFileName());
		}
		List<ITask> addTasks = new ArrayList<>();
		for (String fileName : fileNames) {
			Map<String, Object> params = new HashMap<>();
			params.putAll(request.getParams());
			params.putAll(request.getRestfulParams());
			String encode = EncodingDetect.detect(fileName);
			params.put("encode", encode);
			try (InputStream is = new FileInputStream(fileName);){
				if (FileType.getFileTypeByName(request.getFileName()).equals(FileType.CVS.getTypeName())) {
                    params.put("context", new String(FileUtils.getFileByte(fileName, encode), encode));
				} else {//如果是excel不需要编码
					params.put("context", FileUtils.inputStreamTOString(is, encode));
				}
			} catch (Exception e) {
				BizLogger.error(e);
			}
			FileUtil.del(fileName);// 删除文件
			addTasks.add(new Upload(request.getProtocal(), params));
		}
		MultiAnsyTaskExecute execute = new MultiAnsyTaskExecute(addTasks);
		return execute.supplyAsync();
	}

	public static class Upload implements ITask {
		private final Map<String, Object> params;
		private final GetwayTransfer getWayProtocal;

		public Upload(GetwayTransfer getWayProtocal, Map<String, Object> params) {
			this.getWayProtocal = getWayProtocal;
			this.params = params;
		}
		@Override
		public BeeResponse doTask() {
			return getWayProtocal.invoke(params);
		}
	}

}
